package com.songcha.module_mine.ui.fragment.setting.teenager

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.songcha.library_base.base.BaseFragment
import com.songcha.library_business.provider.IClearToMainActivityProvider
import com.songcha.library_business.proxy.teenager.TeenagerManager
import com.songcha.library_common.util.JumpUtil
import com.songcha.library_common.util.PackageUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.module_mine.databinding.MineFragmentTeenagerBinding
import com.songcha.module_mine.ui.activity.teenager.mode.TeenagerModeActivity


class TeenagerFragment:BaseFragment() {
    init{
        TAG=TeenagerFragment::class.java.simpleName
    }

    private lateinit var binding:MineFragmentTeenagerBinding

    private var mIsExitTeenager=false

    override fun getRootView(): View? {
        binding=MineFragmentTeenagerBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        arguments?.let {
            mIsExitTeenager=it.getBoolean("is_exit_teenager")
        }

        if(mIsExitTeenager){
            binding.mineLlExplain.visibility=View.GONE
            binding.mineLlExitTeenager.visibility=View.VISIBLE
        }else{
            if(TeenagerManager.isTeenagerMode){
                binding.mineLlExplain.visibility=View.GONE
                binding.mineLlAlreadyEnter.visibility=View.VISIBLE
            }
        }

        binding.mineTvTip.apply {
            text = text.toString().replace("\${app_name}", PackageUtil.getAppName(requireContext()))
        }

        binding.mineBtnExplain.setOnClickListener {
            //hideExplain()
            binding.mineLlExplain.visibility=View.GONE
            binding.mineLlEnterTeenager.visibility=View.VISIBLE
        }

        binding.mineBtnEnterTeenager.setOnClickListener {
            enterTeenager()
        }

        binding.mineBtnExitTeenagerAlready.setOnClickListener {
            binding.mineLlAlreadyEnter.visibility=View.GONE
            binding.mineLlExitTeenager.visibility=View.VISIBLE
        }

        binding.mineBtnExitTeenager.setOnClickListener {
            exitTeenager()
        }


    }

    private fun enterTeenager(){
        val pwd=binding.mineEdtPwd.text.toString().trim()
        val pwdConfirm=binding.mineEdtPwdConfirm.text.toString().trim()
        if(!checkPwd(pwd,pwdConfirm)) return
        TeenagerManager.enterTeenagerMode(pwd)
        JumpUtil.toActivityAndClearAll(requireContext(), TeenagerModeActivity::class.java)
        ToastUtil.show("已进入青少年模式")
    }

    private fun exitTeenager(){
        val pwd=binding.mineEdtExitPwd.text.toString().trim()
        //val pwdConfirm=binding.mineEdtExitPwdConfirm.text.toString().trim()
        if(!checkPwd(pwd,pwd)) return
        if(pwd!=TeenagerManager.teenagerPwd){
            ToastUtil.show("密码不正确")
            return
        }
        TeenagerManager.exitTeenagerMode()
        ARouter.getInstance().build("/main/ClearToMainActivityImpl").navigation()?.let {
            it as IClearToMainActivityProvider
            it.clearToMain(false)
        }
        ToastUtil.show("已退出青少年模式")
    }

    private fun checkPwd(pwd:String,pwdConfirm:String):Boolean{
        if(pwd.isEmpty()){
            ToastUtil.show("请输入密码")
            return false
        }
        if(pwdConfirm.isEmpty()){
            ToastUtil.show("请再次确认密码")
            return false
        }
        if(pwd.length<4 || pwdConfirm.length<4 ){
            ToastUtil.show("密码不能少于四位数")
            return false
        }
        if(pwd.length>4 || pwdConfirm.length>4 ){
            ToastUtil.show("密码不能超过四位数")
            return false
        }
        if(pwd!=pwdConfirm){
            ToastUtil.show("密码要保持一致")
            return false
        }
        return true
    }



}